package com.blac.utils

import com.blac.ad.MaxReward
import com.blac.bridge.CocosBridge
import org.json.JSONObject

object ADManager {

    fun playAd(adSource :String){
        try {
            if (!MaxReward.isReady()){
                callbackAdError()
                return
            }
            callback(adSource)
        }catch (e:Exception){
            e.printStackTrace()
            callbackAdError()
        }
    }

    private fun callback(adSource: String){
        val mainGameActivity = CocosBridge.mainGameActivity?.get()?:return
        MaxReward.showAd(mainGameActivity,adSource)
    }

    fun callbackAdOver(adSource: String){
        try {
            val  jsonObject = getJson(adSource)
            CocosBridge.XSSdkCallback("ad_over_wha",jsonObject.toString())
        }   catch (e:Exception){
            e.printStackTrace()
        }
    }

    fun callbackAdError(){
        try {
            CocosBridge.XSSdkCallback("ad_error_wha","{}")
        }catch (e:Exception){
            e.printStackTrace()
        }
    }

    private fun getJson(adSource: String):JSONObject{
        val jsonObject = JSONObject()
        jsonObject.put("ad_source",adSource)
        return jsonObject
    }
}